<template>
  <scroll-view class="container" :scroll-x="false" :scroll-y="true" :scroll-with-animation="true">
    <view class="video-name">
      <input
          border="none"
          class="name-input"
          placeholder="请输入视频名称"
          v-model="formData.title"
      />
    </view>
    <view class="body-clone">
      <view @tap="openBodyPicker" class="selected" v-if="selectedBody && selectedBody.id">
        <image mode="aspectFill" :src="selectedBody.poster"></image>
        <view class="btn-wrap">
          <view class="btn">更换分身</view>
        </view>
      </view>
      <view @tap="openBodyPicker" class="add-btn" v-else>
        <u-icon :bold="true" color="#fff" name="plus" size="74rpx"></u-icon>
        <view class="tips">选择我的分身</view>
      </view>
    </view>
    <view class="content">
      <view class="title-wrap">
        <view class="tabs">
          <view @tap="switchAudioType('tts')" class="item" :class="{active: formData.audio_type === 'tts'}">
            <text>合成配音</text>
          </view>
          <view @tap="switchAudioType('audio')" class="item" :class="{active: formData.audio_type === 'audio'}">
            <text>上传配音</text>
          </view>
        </view>
      </view>
      <view class="textarea-wrap" v-if="formData.audio_type === 'tts'">
          <textarea
              adjustKeyboardTo="bottom"
              @blur="calcPayMoney"
              :disableDefaultPadding="true"
              :maxlength="-1"
              placeholder="请输入文案"
              style="height: 420rpx; font-size: 28rpx; padding: 0; box-sizing: border-box; line-height: 34rpx"
              v-model="text"
          ></textarea>
        <view class="actions">
          <view style="display: flex; align-items: center;">
            <view @tap.stop.prevent="openVoicePicker" class="item" style="margin-right: 20rpx;">
              <image mode="widthFix" src="/static/clone/audio-s.png" v-if="formData.speaker_id"></image>
              <image mode="widthFix" src="/static/clone/audio.png" v-else></image>
              <text>选择音色</text>
            </view>
            <view @tap.stop.prevent="openAiWrite" class="item">
              <image mode="widthFix" src="/static/clone/ic_ai.png"></image>
              <text>写文案</text>
            </view>
          </view>
          <view @tap.stop.prevent="chunkText" class="item">
            <image mode="widthFix" src="/static/clone/ic_cut.png"></image>
            <text>智能断句</text>
          </view>
        </view>
      </view>
      <view class="upload-wrap" v-if="formData.audio_type === 'audio'">
        <view class="upload-list">
          <view class="upload-button" v-if="!uploadObj">
            <view @tap="uploadAudio" class="upload-box default">
              <u-icon :bold="true" color="#fff" name="plus" size="74rpx"></u-icon>
              <view class="tips">
                <!-- #ifdef MP-WEIXIN -->
                <view>从微信聊天选择音频文件</view>
                <!-- #endif -->
                <!-- #ifndef MP-WEIXIN -->
                <view>上传文件</view>
                <!-- #endif -->
                <view>mp3、wav、ogg、aac</view>
                <view>10M以内</view>
              </view>
            </view>
          </view>
          <view class="upload-item" v-else>
            <view class="upload-box success" v-if="uploadObj.url && uploadObj.status === 'success'">
              <view class="play-btn">
                <view @tap="removeUploadAudio" class="close">
                  <u-icon name="close-circle-fill" :size="20"></u-icon>
                </view>
                <image
                    @tap="soundPlay('pause')"
                    mode="widthFix"
                    src="/static/clone/pause-white.png"
                    v-if="uploadObj.playing"
                ></image>
                <image
                    @tap="soundPlay('play', uploadObj)"
                    mode="widthFix"
                    src="/static/clone/play-white.png"
                    v-else
                ></image>
                <view class="time-box" v-if="uploadObj.duration">
                  <image mode="widthFix" src="/static/clone/ic_time_white.png"></image>
                  <view class="time">{{ uploadObj.duration + 's' }}</view>
                </view>
              </view>
              <image class="cover" mode="aspectFill" src="/static/clone/player_bg.jpg"></image>
            </view>

            <view class="upload-box" v-if="uploadObj.status === 'loading'">
              <view class="upload-status">
                <view class="proress">{{ '' + uploadObj.progress + '%' }}</view>
                <view>上传中</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="button">
      <view class="money" @tap="showMoneyTip">
        <text>本次合成预计消耗</text>
        <text class="num">{{ payMoney }}</text>
        <text>{{ money_name }}</text>
        <image class="icon" src="/static/clone/ic_help_tip.png"></image>
      </view>
      <button @tap="doSubmit" class="confirm-btn" :plian="true">提交</button>
    </view>
    <u-popup
        @close="closeBgmPicker"
        round="30rpx"
        :show="bgmPickerShow"
    >
      <view class="audio-selector">
        <view class="title-wrap">
          <view class="title">选择背景音乐</view>
          <view @tap="closeBgmPicker" class="close">
            <u-icon name="close"></u-icon>
          </view>
        </view>
        <view class="tabs">
          <u-tabs
              :list="bgmGroup"
              :value="bgmGroupCurrent"
              @change="bgmGroupChange"
          ></u-tabs>
        </view>
        <view class="list-wrap">
          <view class="list">
            <view
                @tap="chooseBgm(item)"
                class="item"
                :class="{active: selectedBgm && selectedBgm.id === item.id}"
                v-for="(item, index) in bgmList"
                :key="index"
            >
              <view class="check-box">
                <u-icon color="#ffffff" name="checkmark"></u-icon>
              </view>

              <image
                  @tap.stop.prevent="soundPlay('pause')"
                  mode="widthFix"
                  src="/static/clone/ic_audio_pause.png"
                  v-if="item.playing"
              ></image>
              <image
                  @tap.stop.prevent="soundPlay('play', item)"
                  mode="widthFix"
                  src="/static/clone/ic_audio_play.png"
                  v-else
              ></image>

              <view class="name u-line-1">{{ item.title }}</view>
            </view>
          </view>
          <view class="loadmore">
            <u-empty
                icon="/static/ic_empty_list.png"
                :iconSize="46"
                style="padding-top: 60rpx"
                text="暂无音乐"
                v-if="bgmListParams.isEmpty"
            ></u-empty>
            <u-loadmore
                @loadmore="bgmListLoadMore"
                loadmoreText="点击加载更多"
                :status="bgmListParams.status"
                v-else
            ></u-loadmore>
          </view>
        </view>
        <view class="footer">
          <button @tap="clearBgm" class="confirm cancel" :plain="true">取消背景音乐</button>
          <button @tap="confirmBgm" class="confirm" :plain="true">确认</button>
        </view>
      </view>
    </u-popup>
    <u-popup
        @close="closeVoicePicker"
        round="30rpx"
        :show="voicePickerShow"
    >
      <view class="audio-selector">
        <view class="title-wrap">
          <view class="title">选择音色</view>
          <view @tap="closeVoicePicker" class="close">
            <u-icon name="close"></u-icon>
          </view>
        </view>
        <view class="tabs">
          <u-tabs
              :list="voiceGroup"
              :value="voiceGroupCurrent"
              lineColor="#ea4679"
              @change="voiceGroupChange"
          ></u-tabs>
        </view>
        <view class="list-wrap">
          <view class="list">
            <view
                @tap="chooseVoice(item)"
                class="item"
                :class="{active: selectedVoice && selectedVoice.speaker_id === item.speaker_id}"
                v-for="(item, index) in voiceList"
                :key="index"
            >
              <view v-if="item.poster" class="avatar" :style="'background-image:url(' + item.poster + ')'"
                    @tap.stop.prevent="soundPlay('play', item)">
                <view v-if="item.playing" class="pause" @tap.stop.prevent="soundPlay('pause')">
                  <image
                      mode="widthFix"
                      src="/static/clone/ic_audio_pause_white.png"
                  ></image>
                </view>
              </view>
              <view v-else style="margin-left: 20rpx; height: 44rpx;">
                <image
                    @tap.stop.prevent="soundPlay('pause')"
                    mode="widthFix"
                    src="/static/clone/ic_audio_pause.png"
                    v-if="item.playing"
                ></image>
                <image
                    @tap.stop.prevent="soundPlay('play', item)"
                    mode="widthFix"
                    src="/static/clone/ic_audio_play.png"
                    v-else
                ></image>
              </view>
              <view class="name u-line-1">{{ item.title }}</view>
            </view>
          </view>
          <view class="loadmore">
            <u-empty
                icon="/static/ic_empty_list.png"
                :iconSize="46"
                style="padding-top: 60rpx"
                text="暂无音色"
                v-if="voiceListParams.isEmpty"
            ></u-empty>
            <u-loadmore
                @loadmore="voiceListLoadMore"
                loadmoreText="点击加载更多"
                :status="voiceListParams.status"
                v-else
            ></u-loadmore>
          </view>
        </view>
        <view class="footer">
          <button @tap="confirmVoice" class="confirm" :plain="true">确认</button>
        </view>
      </view>
    </u-popup>
    <u-popup
        @close="closeBodyPicker"
        :closeable="true"
        round="30rpx"
        :show="bodyPickerShow"
    >
      <view class="image-selector">
        <view class="title">选择数字分身</view>
        <view class="go-create" v-if="bodyList.length === 0">
          <u-empty
              icon="/static/ic_empty_list.png"
              :text="userBodyFilterList.length > 0 ? userBodyFilterList.length + '个分身正在创建中' : '您还没有形象分身，快去创建一个吧~'"
              width="200rpx"
          ></u-empty>
          <button @tap="goCreateBody" class="btn" :plain="true">创建分身</button>
        </view>
        <scroll-view class="list" :scrollY="true" v-if="userBodyFilterList.length > 0">
          <view @tap="goCreateBody" class="item add-btn">
            <view class="poster">
              <u-icon :bold="true" color="#666" name="plus" size="74rpx"></u-icon>
              <text>创建分身</text>
            </view>
          </view>
          <view
              @tap="chooseBody(item)"
              class="item"
              v-for="(item, index) in userBodyFilterList"
              :key="index"
          >
            <view class="poster">
              <image mode="aspectFill" :src="item.poster"></image>
              <view class="name">{{ item.title }}</view>
            </view>
          </view>
        </scroll-view>
      </view>
    </u-popup>
    <u-popup
        @close="closeMoneyTip"
        :closeOnClickOverlay="true"
        :closeable="true"
        mode="center"
        round="30rpx"
        :safeAreaInsetBottom="false"
        :show="moneyTipShow"
    >
      <view class="money-tip">
        <view class="title">计算规则</view>
        <view class="content" v-if="payConfig">
          <view>1、每10s消耗{{ payConfig.price_video }}{{ money_name }}；</view>
          <view>2、时长不足10s部分按10s计；</view>
          <view v-if="formData.audio_type === 'tts'">3、此费用是根据文案字数预估，最终扣费以合成后的视频时长为准；</view>
        </view>
        <view class="content" v-else>
          <view>正在获取配置</view>
        </view>
      </view>
    </u-popup>
    <ai-write
        @confirm="aiWriteFinish"
        ref="aiWriteRef"
    ></ai-write>
    <protocol
        @close="protocolClose"
        ref="protocol"
    ></protocol>
  </scroll-view>
</template>

<script>
import protocol from '@/components/protocol'
import aiWrite from '@/components/aiWrite'

export default {
  components: { protocol, aiWrite },
  data() {
    return {
      audioPlayer: null,
      // 分身选择
      bodyPickerShow: false,
      bodyList: [],
      selectedBody: {
        id: null,
        name: null,
        url: ''
      },
      bodyPickerParams: {
        page: 1,
        limit: 20,
        status: 'loadmore',
        isEmpty: false
      },
      uploadObj: null,
      // 背景音乐选择
      bgmGroup: [],
      bgmGroupCurrent: 0,
      bgmList: [],
      bgmPickerShow: false,
      selectedBgm: null,
      bgmListParams: {
        page: 1,
        limit: 20,
        status: 'loadmore',
        isEmpty: false
      },
      // 音色选择
      voiceGroup: [],
      voiceGroupCurrent: 0,
      voiceList: [],
      voicePickerShow: false,
      selectedVoice: null,
      voiceListParams: {
        page: 1,
        limit: 20,
        status: 'loadmore',
        isEmpty: false
      },
      effectFontPopupShow: false,
      effectFontList: [], // 字幕字体效果列表
      defaultParam: {},
      audioUploadTypes: ['mp3', 'MP3', 'm4a', 'M4A', 'wav', 'WAV', 'ogg', 'OGG', 'aac', 'AAC'],
      payConfig: null,
      payMoney: 0,
      money_name: '算力',
      text: '', // 文案内容
      formData: {
        title: '',
        audio_type: 'tts', // 声音合成、音频文件 tts or audio
        body_id: '',
        voice_type: '',     // 音色类型 my or public
        speaker_id: '',     // 音色id
        text: '',
        audio: {},          // 上传的音频文件
        speed_rate: 1,
        bgm_volume: 0.2,
        bgm: '',
        show_subtitle: 0
      },
      moneyTipShow: false
    }
  },
  onLoad() {
    this.getDefaultParam()
    this.getPrice()
    this.getBgmGroup()
    this.getVoiceGroup()
  },
  onHide() {
    this.soundPlay('pause');
  },
  beforeDestroy() {
    this.soundPlay('pause');
  },
  computed: {
    userBodyFilterList() {
      return this.bodyList.filter(item => {
        return item.status === 1;
      })
    }
  },
  methods: {
    goCreateBody() {
      var that = this;
      this.bodyPickerShow = false;
      uni.navigateTo({
        url: '/pages/clone/create-body',
        events: {
          created() {
            that.getBodyList();
          }
        }
      });
    },
    getDefaultParam() {
      this.$util.request({
        url: '/zuopin/getDefaultParam',
        loading: false
      }).then(res => {
        this.defaultParam = res.data;
        Object.keys(this.defaultParam).forEach(key => {
          if (key in this.formData) {
            this.formData[key] = this.defaultParam[key];
          }
        });
      })
    },
    getPrice() {
      this.$util.request({
        url: '/zuopin/getPrice',
        loading: false
      }).then(res => {
        this.payConfig = res.data;
      })
    },
    getBgmGroup() {
      this.$util.request({
        url: '/zuopin/getBgmGroup',
        loading: false
      }).then(res => {
        this.bgmGroup = res.data.map(item => {
          return {
            id: item.id,
            name: item.title
          }
        })
      })
    },
    getVoiceGroup() {
      this.$util.request({
        url: '/zuopin/getVoiceGroup',
        loading: false
      }).then(res => {
        this.voiceGroup = res.data.map(item => {
          return {
            id: item.id,
            name: item.title
          }
        })
      })
    },
    openBodyPicker() {
      this.list = []
      this.bodyPickerParams.page = 1
      this.getBodyList()
      this.bodyPickerShow = true;
    },
    closeBodyPicker() {
      this.bodyPickerShow = false;
    },
    getBodyList() {
      this.$util.request({
        url: '/userBody/getList',
        data: {
          page: this.bodyPickerParams.page,
          limit: this.bodyPickerParams.limit
        }
      }).then(res => {
        this.bodyList = res.data.list
      })
    },
    chooseBody(item) {
      this.selectedBody = item;
      this.formData.body_id = item.id;
      this.bodyPickerShow = false;
    },
    switchAudioType(type) {
      this.formData.audio_type = type
    },
    switchSubtitleShow() {
      this.formData.show_subtitle = this.formData.show_subtitle ? 0 : 1
    },
    // 以下为背景音乐选择一系列方法
    openBgmPicker() {
      this.bgmPickerShow = true
      this.bgmGroupCurrent = 0
      this.bgmList = []
      this.bgmListParams = {
        page: 1,
        limit: 20,
        status: 'loadmore',
        isEmpty: false
      }
      this.getBgmList()
    },
    closeBgmPicker() {
      this.soundPlay('pause')
      this.bgmPickerShow = false
    },
    bgmGroupChange(item) {
      var that = this;
      that.bgmGroupCurrent = item.index;
      that.bgmList = []
      that.bgmListParams.page = 1
      that.getBgmList()
    },
    getBgmList() {
      this.bgmListParams.isEmpty = false;
      this.bgmListParams.status = 'loading';
      this.$util.request({
        url: '/zuopin/getBgmList',
        data: {
          group_id: this.bgmGroup[this.bgmGroupCurrent].id,
          page: this.bgmListParams.page,
          limit: this.bgmListParams.limit
        },
        loading: false
      }).then(res => {
        this.bgmListParams.total = res.data.total;
        this.bgmList = this.bgmList.concat(
            res.data.list.map(item => {
              item.playing = false
              return item
            })
        );
        if (res.data.total === 0) {
          this.bgmListParams.isEmpty = true
        }
        this.bgmListParams.status = this.bgmList.length < res.data.total ? 'loadmore' : 'nomore';
      }).catch(err => {
        this.$util.message(err.message)
      })
    },
    bgmListLoadMore() {
      this.bgmListParams.page += 1;
      this.getBgmList();
    },
    chooseBgm(item) {
      if (this.selectedBgm && this.selectedBgm.id === item.id) {
        this.selectedBgm = null
      } else {
        this.selectedBgm = item
      }
    },
    confirmBgm() {
      if (this.selectedBgm && this.selectedBgm.id) {
        this.formData.bgm = this.selectedBgm.url
      } else {
        this.selectedBgm = null
        this.formData.bgm = ''
      }
      this.closeBgmPicker()
    },
    clearBgm() {
      this.selectedBgm = null
      this.formData.bgm = ''
      this.closeBgmPicker()
    },
    // 以下为音色选择一系列方法
    openVoicePicker() {
      this.voicePickerShow = true
      this.voiceGroupCurrent = 0
      this.voiceList = []
      this.voiceListParams = {
        page: 1,
        limit: 20,
        status: 'loadmore',
        isEmpty: false
      }
      this.getVoiceList()
    },
    closeVoicePicker() {
      this.soundPlay('pause')
      this.voicePickerShow = false
    },
    voiceGroupChange(item) {
      this.voiceGroupCurrent = item.index;
      this.voiceList = []
      this.voiceListParams.page = 1
      this.getVoiceList()
    },
    getVoiceList() {
      this.voiceListParams.isEmpty = false;
      this.voiceListParams.status = 'loading';
      this.$util.request({
        url: '/zuopin/getVoiceList',
        data: {
          group_id: this.voiceGroup[this.voiceGroupCurrent].id,
          page: this.voiceListParams.page,
          limit: this.voiceListParams.limit
        },
        loading: false
      }).then(res => {
        this.voiceListParams.total = res.data.total;
        this.voiceList = this.voiceList.concat(
            res.data.list.map(item => {
              item.playing = false
              return item
            })
        );
        if (res.data.total === 0) {
          this.voiceListParams.isEmpty = true
        }
        this.voiceListParams.status = this.voiceList.length < res.data.total ? 'loadmore' : 'nomore';
      }).catch(err => {
        this.$util.message(err.message)
      })
    },
    voiceListLoadMore() {
      this.voiceListParams.page += 1;
      this.getVoiceList();
    },
    chooseVoice(item) {
      if (this.selectedVoice && this.selectedVoice.speaker_id === item.speaker_id) {
        this.selectedVoice = null
      } else {
        this.selectedVoice = item
      }
    },
    confirmVoice() {
      if (!this.selectedVoice || !this.selectedVoice.speaker_id) {
        this.$util.message('请选择音色')
        return
      }
      if (this.voiceGroupCurrent === 0) {
        this.formData.voice_type = 'my'
      } else {
        this.formData.voice_type = 'public'
      }
      this.formData.speaker_id = this.selectedVoice.speaker_id
      this.closeVoicePicker()
    },
    openAiWrite() {
      // this.$util.message('即将上线', 'error')
      this.$refs.aiWriteRef.popupShow = true;
    },
    closeAiWrite() {
      this.$refs.aiWriteRef.popupShow = false;
    },
    aiWriteFinish(content) {
      this.text = content;
      this.calcPayMoney()
    },
    uploadAudio() {
      var that = this;
      var data = {
        filePath: '',
        fileName: '',
        fileSize: 0
      };
      var chooseFunc = uni.chooseFile;
      if (uni.chooseMessageFile) {
        chooseFunc = uni.chooseMessageFile;
      }
      chooseFunc({
        count: 1,
        type: 'file',
        extension: this.audioUploadTypes,
        success: function (file) {
          data.filePath = file.tempFiles[0].path;
          data.fileSize = file.tempFiles[0].size;
          data.fileName = file.tempFiles[0].name;
          if (data.fileSize / 1024 / 1024 > 10) {
            setTimeout(() => {
              that.$util.message('请上传10M以内的文件')
            }, 500)
            return
          }
          that.uploadObj = {
            status: 'loading',
            progress: 0
          }
          that.$util.upload({
            url: '/zuopin/uploadAudio',
            filePath: data.filePath,
            name: 'audio',
            onProgressUpdate: function (res) {
              that.uploadObj.progress = res.progress;
            }
          }).then(res => {
            uni.showLoading({
              title: '解析中'
            })
            that.uploadObj.status = 'success';
            that.uploadObj.url = res.url;
            that.formData.audio = {
              url: res.url,
              size: data.fileSize
            }
            const audioContext = uni.createInnerAudioContext();
            audioContext.volume = 100;
            audioContext.src = res.url;
            audioContext.onTimeUpdate((e) => {
              if (audioContext.duration) {
                audioContext.stop()
                audioContext.offTimeUpdate()
                audioContext.volume = 100;
                uni.hideLoading()
                var duration = Math.ceil(audioContext.duration)
                that.formData.audio.duration = duration
                that.$set(that.uploadObj, 'duration', duration)
                that.calcPayMoney()
              }
            })
            audioContext.onError(() => {
              that.removeUploadAudio()
              that.hideLoading()
              that.$util.message('音频加载失败，请重新上传')
            })
            audioContext.onCanplay(() => {
              audioContext.play();
            });

          }).catch(err => {
            that.uploadObj = null
          })
        },
        fail: function (err) {
          console.log('选择文件失败：', err);
        }
      });
    },
    removeUploadAudio() {
      this.soundPlay('pause');
      this.uploadObj = null;
      this.formData.audio = {};
      this.calcPayMoney()
    },
    protocolClose() {
      uni.navigateBack({
        fail: function (err) {
          uni.reLaunch({
            url: '/pages/index/index'
          })
        }
      });
    },
    soundPlay(action, audio = null) {
      // 停止播放所有
      if (this.audioPlayer) {
        try {
          this.audioPlayer.pause();
          this.audioPlayer.destroy();
          this.audioPlayer = null;
        } catch (err) {
          console.log('soundPlay', err);
        }
      }
      // 所有状态置为未播放
      this.bgmList.forEach(item => {
        item.playing = false;
      });
      this.voiceList.forEach(item => {
        item.playing = false;
      });
      if (this.uploadObj) {
        this.uploadObj.playing = false;
      }
      // 如果action==play开始播放当前
      if (action === 'play' && audio) {
        audio.playing = true;
        this.audioPlayer = uni.createInnerAudioContext();
        this.audioPlayer.autoplay = true;
        this.audioPlayer.src = audio.demo_url || audio.url;
      }
    },
    calcPayMoney() {
      this.payMoney = 0;
      var duration = 0
      if (this.formData.audio_type === 'tts') {
        var text = this.textFormat(this.text);
        if (text.length === 0) {
          this.payMoney = 0;
        }
        duration = Math.ceil(text.length / 5) // 大概5个字1秒
      } else if (this.formData.audio && this.formData.audio.duration) {
        duration = Math.ceil(this.formData.audio.duration)
      }
      // 每10s的价格
      this.payMoney = Math.ceil(Math.ceil(duration / 10) * parseInt(this.payConfig.price_video))
    },
    chunkText() {
      if (!this.text) {
        this.$util.message('请输入文案内容')
        return
      }
      this.text = this.text
          .replace(/[，,！!。；;：:—()（）“”《》、?？]/g, '\n')
          .split('\n')
          .filter(res => {
            return res != '';
          })
          .join('\n')
      this.$util.message('已断句')
    },

    doSubmit() {
      if (!this.formData.body_id) {
        this.$util.message('请选择数字分身')
        return
      }
      if (!this.formData.title) {
        this.$util.message('请设置视频名称')
        return
      }
      if (this.formData.audio_type === 'tts') {
        if (!this.text) {
          this.$util.message('请输入文案内容')
          return
        }
        if (!this.selectedVoice || !this.selectedVoice.speaker_id) {
          this.$util.message('请选择音色')
          return
        }
      } else if (this.formData.type === 'audio') {
        if (!this.formData.audio.url) {
          this.$util.message('请上传语音文件')
          return
        }
        if (this.formData.audio.size >= 30 * 1024 * 1024) {
          this.$util.message('请上传30M以内的文件')
          return
        }
        if (this.formData.audio.duration < 10) {
          this.$util.message('请上传10s以上的音频文件')
          return
        }
      }

      var formData = JSON.parse(JSON.stringify(this.formData));
      formData.text = this.textFormat(this.text);
      formData.audio = JSON.stringify(formData.audio)

      this.$util.request({
        url: '/zuopin/create',
        data: formData,
        loadingText: '正在提交'
      }).then(res => {
        this.$util.message(res.message, 'success')
        setTimeout(() => {
          uni.reLaunch({
            url: '/pages/zuopin/index'
          });
        }, 1500);
      });
    },
    textFormat(text) {
      text = text
          .replace(/[，,！!。；;：:—()（）“”《》、?？]/g, '\n')
          .split('\n')
          .filter(item => {
            return item !== '';
          })
          .join('，');
      return text
    },
    showMoneyTip() {
      this.moneyTipShow = true
    },
    closeMoneyTip() {
      this.moneyTipShow = false
    },
  }
}
</script>
<style scoped>
@import './create.css';
</style>
